package com.eolcum.evo.thread;

import java.io.IOException;

import android.content.Context;
import android.database.SQLException;

import com.eolcum.evo.persistence.Apartment;
import com.eolcum.evo.persistence.DataBaseHelper;
import com.eolcum.evo.util.Constants;

public class HomeController implements Runnable {

	private Apartment apartment = new Apartment();
	private DataBaseHelper dbHelper;
	private Context context;
	@Override
	public void run() {
		dbHelper = new DataBaseHelper(context);

		try {
			dbHelper.createDataBase();
		} catch (IOException ioe) {
			throw new Error("Unable to create database");
		}

		try {
			dbHelper.openDataBase();
		} catch (SQLException sqle) {
			throw sqle;
		}
		if (apartment != null) {
			if (apartment.getRooms() != null && apartment.getRooms().size() > 0) {
				for (int i = 0; i < apartment.getRooms().size(); i++) {
					RoomTemperatureController rc = new RoomTemperatureController();
					rc.setRoom(apartment.getRooms().get(i));
					new Thread(rc, Constants.ROOM_TEMP_CONT_THREAD_NAME_PREFIX + apartment.getRooms().get(i).getId()).start();

					TimeProgramController tpc = new TimeProgramController();
					tpc.setRoom(apartment.getRooms().get(i));
					tpc.setDbHelper(dbHelper);
					new Thread(tpc, Constants.TIME_PROGRAM_CONT_THREAD_NAME_PREFIX + apartment.getRooms().get(i).getId()).start();

					Logger lgr = new Logger();
					lgr.setContext(context);
					lgr.setRtc(rc);
					new Thread(lgr, Constants.LOGGER_THREAD_NAME_PREFIX + apartment.getRooms().get(i).getId()).start();
				}
			}
//			if(hasOccupancySensor()) {
//				OccupancyController oc = new OccupancyController();
//				oc.setApartment(apartment);
//				new Thread(oc,Constants.OCCUPANCY_CONT_THREAD_NAME_PREFIX + apartment.getId()).start();
//			}
		}
		while (true) {
			try {
				if (dbHelper.getHasSystemUpdate()) {
					apartment = dbHelper.getApartment();
					dbHelper.setHasSystemUpdate(false);
					if (apartment == null) {
						break;
					}
				}

				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public synchronized Apartment getApartment() {
		return apartment;
	}

	/**
	 * @param apartment
	 *            the apartment to set
	 */
	public synchronized void setApartment(Apartment apartment) {
		this.apartment = apartment;
	}

	public DataBaseHelper getDbHelper() {
		return dbHelper;
	}

	public void setDbHelper(DataBaseHelper dbHelper) {
		this.dbHelper = dbHelper;
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}
	
	private boolean hasOccupancySensor() {
		if(apartment == null) return false;
		
		for(int i = 0; i < apartment.getRooms().size(); i++) {
			for(int j = 0; j < apartment.getRooms().get(i).getSensors().size();j++) {
				if(apartment.getRooms().get(i).getSensors().get(j).isHasOccup())
					return true;
			}
		}
		return false;
	}
}
